<?php
/**
 * Created by PhpStorm.
 *
 * Author : Traveler.
 * Time   : 2020/3/11 14:15
 * Company: Inner Mongolia Zhengzhi Network Technology Co., Ltd
 */

namespace app\admin\controller;


use app\admin\BaseController;
use app\admin\model\Icafe as IcafeModel;
use app\admin\model\IcafeMirror as IcafeMirrorModel;
use app\admin\middleware\Login;

class IcafeMirror extends BaseController
{
    /**
     * 控制器中间件
     * @var array
     */
    protected $middleware = [Login::class];

    /**
     * 接口：获取端口镜像(表格渲染)
     * @return \think\response\Json
     */
    public function getTableData()
    {
        $param = $this->request->param();
        $data  = IcafeMirrorModel::getIcafeMirrorTable($param);

        return $this->success('获取端口镜像信息成功', $data['data'], ['count' => $data['count']]);
    }

    /**
     * 资料保存：端口镜像
     * @return \think\response\Json
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $icafe_id = $this->request->get('icafe_id');
            $data = $this->request->post();

            $icafe = IcafeModel::find($icafe_id);
            if (!$icafe) {
                return $this->error('场所不存在');
            }

            // 防止重复新增
            if ($icafe->icafeMirror) {
                $data['id'] = $icafe->icafeMirror->id;
            }

            // 关联保存
            $result = $icafe->icafeMirror()->save($data);
            if ($result) {
                return $this->success('保存成功');
            }

            return $this->error('保存失败');
        }

        return $this->error('非法请求');
    }
}